package com.cliang.tools.page;

import java.util.List;

/**
 * 分页工具类
 * @param <T>  需要获取的数据的数据类型
 */
public class PagePartition<T> {
    /**
     * 分页的已知数据
     */
    private int pageSize;       //每页显示的条数
    private int currentPage;    //当前页
    private int totalRecord;    //总的记录数，查询数据库获取

    private int totalPage;      //总页数，根据totalRecord和ageSize计算
    private int startIndex;     //查询数据开始索引，即从数据库中第几行开始拿取数据(limit 语句的数据)

    private List<T> list;       //查询结果数据

    //分页显示的页数,比如在页面上显示1，2，3，4，5页，start就为1，end就为5，这个也是算过来的
    private int start;
    private int end;

    //上一页，下一页，显示数据
    private int prePage;
    private int nextPage;

    public PagePartition() {
        pageSize=20;        //每页显示条数默认为20条
        currentPage=1;      //默认为第一页
        totalPage=0;
        start=1;
        end=5;
        prePage=1;
        nextPage=1;
    }

    public PagePartition(int currentPage,int pageSize,  int totalRecord) {
        this.pageSize = pageSize;
        this.currentPage = currentPage;
        this.totalRecord = totalRecord;

        //总页数
        this.totalPage=(totalRecord%pageSize==0) ? totalRecord/pageSize : totalRecord/pageSize+1;
        //开始查询索引
        this.startIndex=(currentPage-1)*pageSize;

        //分页显示计算
        this.start=1;
        this.end=5;

        if(totalPage<=5){
            //总页数都小于5，那么end就为总页数的值了。
            this.end = this.totalPage;
        }else{
           //总页数大于5，那么就要根据当前是第几页，来判断start和end为多少了，
           this.start = currentPage - 2;
           this.end = currentPage + 2;

           if(start < 0){
             //比如当前页是第1页，或者第2页，那么就不如和这个规则，
                this.start = 1;
                this.end = 5;
               }
            if(end > this.totalPage){
              //比如当前页是倒数第2页或者最后一页，也同样不符合上面这个规则
               this.end = totalPage;
               this.start = end - 5;
               }
        }

        //上一页，下一页
        prePage=(currentPage-1)>0 ? (currentPage-1):1;
        nextPage=currentPage+1>=totalPage ? totalPage: currentPage+1;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getTotalRecord() {
        return totalRecord;
    }

    public void setTotalRecord(int totalRecord) {
        this.totalRecord = totalRecord;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getStartIndex() {
        return startIndex;
    }

    public void setStartIndex(int startIndex) {
        this.startIndex = startIndex;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getEnd() {
        return end;
    }

    public void setEnd(int end) {
        this.end = end;
    }

    public int getPrePage() {
        return prePage;
    }

    public void setPrePage(int prePage) {
        this.prePage = prePage;
    }

    public int getNextPage() {
        return nextPage;
    }

    public void setNextPage(int nextPage) {
        this.nextPage = nextPage;
    }
}
